myApp.controller('managerAdminNodeListCtrl',function($scope,$serMaganerNodeAdmin,$uibModal,$log,toaster,$localStorage) {
    $scope.vm = {
        data: {},
        page: {}
    };
    $scope.model = {
        save:function(state,data){
            if (state == 0) {
                $scope.model.info = {
                    token : $localStorage.userInfo&&$localStorage.userInfo.token,
                    state:state,
                    info:"",
                    node:"",
                    parents:data==0?0:'',
                    remarks:"",
                    sort:1,
                    type:data==0?'控制器':'方法'
                };
            }
            if (state == 1 && data) {
                $scope.model.info = {
                    token : $localStorage.userInfo&&$localStorage.userInfo.token,
                    id:data.id,
                    state:state,
                    info:data.info,
                    node:data.node,
                    parents:data.parents,
                    remarks:data.remarks,
                    sort:data.sort,
                    type:data.type
                };
                $scope.model.info.state = state;
            }
            var modalInstance = $uibModal.open({
                templateUrl: 'managerAdminNodeModal.html',
                controller: 'managerAdminNodeItemModalSaveCtrl',
                windowClass: 'managerAdminNodeItemSave',
                resolve: {
                    info: function () {
                        return $scope.model.info;
                    }
                }
            });
            modalInstance.result.then(function (res) {
                init();
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        },
        del:function(id){
            var modalInstance = $uibModal.open({
                template: '<div class="modal-header"><h3 class="modal-title">确定要删除吗？</h3></div>' +
                '<div class="modal-footer"><button class="btn btn-default" ng-click="cancel()">Cancel</button>' +
                '<button class="btn btn-primary" ng-click="ok()">OK</button></div>',
                controller: 'managerAdminNodeItemModalDelCtrl',
                windowClass: 'managerAdminNodeItemDel',
                size: 'sm'
            });
            modalInstance.result.then(function (res) {
                var type='';
                if(res){
                    $serMaganerNodeAdmin.del({
                        token : $localStorage.userInfo&&$localStorage.userInfo.token,
                        id:id
                    }).then(function(res){
                        switch (res.code){
                            case 1:type = 'success';break;
                            default:type = 'error';break;
                        }
                        toaster.pop(type, '信息', res.message);
                        init($scope.vm.key);
                    })
                }
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        }
    };

    /*跳转页面*/
    $scope.pageChanged = function() {
        init($scope.vm.page.currentpage-1);
    };
    /*每页显示数量显示*/
    $scope.placement = {
        options: [5,10,20,50],
        pageSize:5,
        selected: 10,
        changeFn:function(){
            init(0,0);
        }
    };
    /*初始化数据*/
    function init(page){
        $serMaganerNodeAdmin.list({
            token : $localStorage.userInfo&&$localStorage.userInfo.token,
            page: page||0,
            limit: $scope.placement.selected
        }).then(function (res) {
            $scope.vm.data = res.data;
            if(!page||page==0){$scope.vm.page = res.addtion_data;}
        });
    }
    init();
});
myApp.controller('managerAdminNodeItemModalSaveCtrl', function($scope,$uibModalInstance,info,$serMaganerNodeAdmin,toaster,$localStorage) {
    $serMaganerNodeAdmin.ctrlList({
        token : $localStorage.userInfo&&$localStorage.userInfo.token,
    }).then(function(res){
        if(res.code==1){
              $scope.parent = res.data;
        }
    });
    $scope.info = info;
    $scope.ok = function () {
        if(!$scope.info.node){toaster.info('警告','名字不能为空');return false;}
        if(!$scope.info.info){toaster.info('警告','标题不能为空');return false;}
        if($scope.info.type=='方法'&&$scope.info.parents==''){toaster.info('警告','父节点不能为空');return false;}
        if(!$scope.info.remarks){toaster.info('警告','备注不能为空');return false;}
        if($scope.info.state){/*修改*/
            $serMaganerNodeAdmin.save({
                token : $localStorage.userInfo&&$localStorage.userInfo.token,
                id:$scope.info.id,
                nodeType:$scope.info.type=='控制器'?1:2,
                node:$scope.info.node,
                info:$scope.info.info,
                srot:$scope.info.srot,
                remarks:$scope.info.remarks,
                parents:$scope.info.parents
            }).then(function (res) {
                if(res.code==1){
                    toaster.success('成功',res.message);
                    $uibModalInstance.close(true);
                    return false;
                }else {
                    toaster.error('错误',res.message);
                }
            })
        }else {/*添加*/
            $serMaganerNodeAdmin.add({
                token : $localStorage.userInfo&&$localStorage.userInfo.token,
                nodeType:$scope.info.type=='控制器'?1:2,
                node:$scope.info.node,
                info:$scope.info.info,
                srot:$scope.info.srot,
                remarks:$scope.info.remarks,
                parents:$scope.info.parents
            }).then(function (res) {
                if(res.code==1){
                    toaster.success('成功',res.message);
                    $uibModalInstance.close(true);
                    return false;
                }else {
                    toaster.error('错误',res.message);
                }
            })
        }
    };
    $scope.cancel = function () {
        $uibModalInstance.dismiss(0);
    };
});
myApp.controller('managerAdminNodeItemModalDelCtrl', ['$scope', '$uibModalInstance','$localStorage', function($scope,$uibModalInstance,$localStorage) {
    $scope.ok = function () {
        $uibModalInstance.close(true);
    };
    $scope.cancel = function () {
        $uibModalInstance.dismiss(0);
    };
}]);
/*节点树*/
myApp.controller('managerAdminNodeTreeCtrl',function($scope,$serMaganerNodeAdmin,$localStorage) {

    $scope.vm = {
        data: {}
    };

    /*初始化数据*/
    function init(){
        $serMaganerNodeAdmin.tree({
                token : $localStorage.userInfo&&$localStorage.userInfo.token,
            }
        ).then(function (res) {
            $scope.vm.data = res.data;
        });
    }
    init();
});